package com.starchen.optimalselection.common.exception;

import com.starchen.optimalselection.common.utils.ResponseEnum;
import com.starchen.optimalselection.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.nio.file.AccessDeniedException;
import java.util.List;

@Slf4j
@RestControllerAdvice(basePackages = "com.starchen.optimalselection")
public class GlobalExceptionHandler {

    //1 全局异常
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        log.error(e.getMessage(), e);
        return Result.error();
    }

    //3 自定义异常处理
    @ExceptionHandler(CustomException.class)
    public Result error(CustomException e) {
        e.printStackTrace();
        return Result.error(e.getCode(),e.getMessage());
    }

    //3 自定义异常处理
    @ExceptionHandler(AccessDeniedException.class)
    public Result error(AccessDeniedException e) {
        e.printStackTrace();
        return Result.error(ResponseEnum.PERMISSION);
    }


    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleVaildException(MethodArgumentNotValidException e){
        log.error("数据校验出现问题{}，异常类型：{}",e.getMessage(),e.getClass());
        BindingResult bindingResult = e.getBindingResult();
        List<FieldError> errorList = bindingResult.getFieldErrors();

        String errorMessage = "";
        for(FieldError fieldError: errorList){
            errorMessage = fieldError.getDefaultMessage();
            break;
        }
        return Result.error(ResponseEnum.ARGUMENT_VALID_ERROR.getCode(),errorMessage);
    }

    /**
     * Controller上一层相关异常
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public Result handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.error(ResponseEnum.SERVLET_ERROR);
    }
}
